package com.lswms.modules.lswms.controller.stock;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lswms.common.annotation.LogOperation;
import com.lswms.common.constant.Constant;
import com.lswms.common.page.PageData;
import com.lswms.common.utils.ExcelUtils;
import com.lswms.common.utils.Result;
import com.lswms.common.validator.AssertUtils;
import com.lswms.common.validator.ValidatorUtils;
import com.lswms.common.validator.group.AddGroup;
import com.lswms.common.validator.group.DefaultGroup;
import com.lswms.common.validator.group.UpdateGroup;
import com.lswms.modules.lswms.MEGConstant;
import com.lswms.modules.lswms.dto.base.SkuDTO;
import com.lswms.modules.lswms.dto.stock.InventoryDTO;
import com.lswms.modules.lswms.entity.stock.InventoryEntity;
import com.lswms.modules.lswms.excel.stock.InventoryExcel;
import com.lswms.modules.lswms.service.base.CargojcService;
import com.lswms.modules.lswms.service.base.JianceService;
import com.lswms.modules.lswms.service.base.SkuService;
import com.lswms.modules.lswms.service.impl.base.JianceServiceImpl;
import com.lswms.modules.lswms.service.stock.InventoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 属性
 *
 * @author DX
 * @since 1.0.0 2021-06-14
 */
@RestController
@RequestMapping("lswms/inventory")
@Api(tags = "属性")
public class InventoryController {
	@Autowired
	private InventoryService inventoryService;

	@Autowired
	private SkuService skuService;

	@Autowired
	private JianceService jianceService;

	@Autowired
	private CargojcService cargojcService;

	@GetMapping("page")
	@ApiOperation("分页")
	@ApiImplicitParams({
			@ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
			@ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
			@ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String") })
//	@RequiresPermissions("lswms:inventory:page")
	public Result<PageData<InventoryEntity>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
		PageData<InventoryEntity> page = inventoryService.pageBySql(params);
		
		return new Result<PageData<InventoryEntity>>().ok(page);
	}
	@GetMapping("cargo")
	@ApiOperation("检测箱")
	@ApiImplicitParams({
			@ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
			@ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
			@ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String") })
//	@RequiresPermissions("lswms:inventory:page")
	public Result<PageData<InventoryEntity>> page2(@ApiIgnore @RequestParam Map<String, Object> params) {
		PageData<InventoryEntity> page = inventoryService.pageBySql2(params);

		return new Result<PageData<InventoryEntity>>().ok(page);
	}

	@GetMapping("cargodetail")
	@ApiOperation("检测箱明细")
	@ApiImplicitParams({
			@ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
			@ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
			@ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String") })
//	@RequiresPermissions("lswms:inventory:page")
	public Result<PageData<InventoryEntity>> cargodetail(@ApiIgnore @RequestParam Map<String, Object> params) {
		PageData<InventoryEntity> page = inventoryService.pageBySql3(params);

		return new Result<PageData<InventoryEntity>>().ok(page);
	}

	@GetMapping("byData")
	@ApiOperation("分页")
	public Result<List<InventoryEntity>> byData(@ApiIgnore @RequestParam Map<String, Object> params) {
		List<InventoryEntity> list = inventoryService.byData(params);
		
		return new Result<List<InventoryEntity>>().ok(list);
	}
	
	
	@GetMapping("{id}")
	@ApiOperation("信息")
//	@RequiresPermissions("lswms:inventory:info")
	public Result<InventoryDTO> get(@PathVariable("id") Long id) {
		InventoryDTO data = inventoryService.get(id);

		return new Result<InventoryDTO>().ok(data);
	}

	@PostMapping
	@ApiOperation("保存")
	@LogOperation("保存")
//	@RequiresPermissions("lswms:inventory:save")
	public Result save(@RequestBody InventoryDTO dto) {
		// 效验数据
		ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

		inventoryService.save(dto);

		return new Result();
	}

	@PutMapping
	@ApiOperation("修改")
	@LogOperation("修改")
//	@RequiresPermissions("lswms:inventory:update")
	public Result update(@RequestBody InventoryDTO dto) {
		// 效验数据
		ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

		inventoryService.update(dto);

		return new Result();
	}

	@DeleteMapping
	@ApiOperation("删除")
	@LogOperation("删除")
//	@RequiresPermissions("lswms:inventory:delete")
	public Result delete(@RequestBody Long[] ids) {
		// 效验数据
		AssertUtils.isArrayEmpty(ids, "id");

		inventoryService.delete(ids);

		return new Result();
	}

	@GetMapping("export")
	@ApiOperation("导出")
	@LogOperation("导出")
//	@RequiresPermissions("lswms:inventory:export")
	public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response)
			throws Exception {
		//List<InventoryDTO> list = inventoryService.list(params);
		List<InventoryEntity> list = inventoryService.byData(params);
		
		ExcelUtils.exportExcelToTarget(response, null, list, InventoryExcel.class);
	}

	@PostMapping("transferTray")
	@ApiOperation("移箱")
	@LogOperation("移箱")
	public Result transferTray(@RequestBody List<InventoryEntity> list) {
		// 效验数据
		// ValidatorUtils.validateEntity(entity, AddGroup.class, DefaultGroup.class);
		try {
			String msg = inventoryService.transferTray(list);

			if (StringUtils.isEmpty(msg)) {
				return new Result();
			} else {
				return new Result().error(msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result().error(MEGConstant.IS_ERROR);
		}
	}

	@PostMapping("cargoDatail")
	@ApiOperation("检测完毕")
	@LogOperation("检测完毕")
	public Result cargoDatail(@RequestBody List<InventoryEntity> list) {
		// 效验数据
		// ValidatorUtils.validateEntity(entity, AddGroup.class, DefaultGroup.class);
		try {


			String msg = inventoryService.transferTray(list);

			if (StringUtils.isEmpty(msg)) {
				//在原有的箱转移功能上，把物料的检测状态给清楚掉
				cargojcService.cargoDatail(list);
				return new Result();
			} else {
				return new Result().error(msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result().error(MEGConstant.IS_ERROR);
		}
	}

	@PostMapping("jiance")
	@ApiOperation("检测")
	@LogOperation("检测")
	public Result detection(@RequestBody List<String> ids)
	{
		try{
			String msg = "";

			for (Object iid: ids) {
				//Map<String,Object> map = JSONObject.parseObject(JSON.toJSONString(iid));
				String id = iid.toString();
				msg = jianceService.detectionSelect(id);
				if(msg.length()<1){
					msg = "id:"+id+"检测失败!";
					break;
				}
			}

			return new Result().ok(msg);
		}catch (Exception e){
			e.printStackTrace();
			return new Result().error(MEGConstant.IS_ERROR);
		}
	}


}
